package com.August_ThirtyOne;


import java.util.*;

public class Solution144 {

    /**
     * 使用迭代法实现二叉树的先序遍历
     * @param root
     * @return
     */

    public List<Integer> preorderTraversal(TreeNode root) {
        Deque<TreeNode> stack=new ArrayDeque<>();
        List<Integer> res=new ArrayList<>();
        if (root==null) return res;
        stack.push(root);
        while (!stack.isEmpty())
        {
            TreeNode node=stack.pollFirst();
            System.out.println(node.val);
            res.add(node.val);
            if (node.left!=null) stack.push(node.left);
            if(node.right!=null) stack.push(node.right);
        }
        return res;
    }
}

 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
}
